<!-- 传阅人选择项目人员审批组件 -->
<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-row>
      <el-col :span="24">
        <el-button type="danger" style="float: right" size="small" icon="el-icon-close" circle @click="doCancelEdit"></el-button>
      </el-col>
    </el-row>
    <el-form class="tl-form" style="margin-top: 15px" ref="form" :model="memberModel" :rules="rules" label-width="120px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="24">
          <el-form-item label="需求说明" prop="requirementdescription">
            <el-input v-model="memberModel.requirementdescription" type="textarea" :rows="4" maxlength="1000" show-word-limit disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="项目人员" prop="projectmembercode" :rules="rules.validateRequire">
            <tl-ref-user v-model="memberModel.projectmembercode" :initDisplayData.sync="memberModel.projectmembername" dataType="code" @confirm="confirmMember" :singleSelect="false"></tl-ref-user>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <tl-project-info ref="projectInfo"></tl-project-info>
    </el-form>
    <tl-approval-history ref="history" sourceType="MpmProjectCirculated" :sourceId="applyid" />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="$refs.history.visible = true"><svg-icon icon-class="list" />审批历史</el-button>
    </div>
  </tl-sub-page>
</template>

<script>
  /** mixins */
  import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
  /** 子组件 */
  import tlProjectInfo from '../mpmProject/circulatedEditComp'
  /** 工具类 */
  import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
  /** VUE配置 */
  export default {
    mixins: [mixin_approval_edit],
    components: { tlProjectInfo },
    data() {
      return {
        moduleTitle: '项目传阅反馈人员',
        urlPrefix: '/mpm/projectCirculatedMember/',
        url: {
          // 额外的url
          getRequirementdescription: '/mpm/projectCirculated/load/',
          saveMemberData: '/mpm/projectCirculatedMember/saveMemberData/',
          queryProjectList: '/mpm/project/list'
        },
        memberModel: {},
        applyid: 0
      }
    },
    methods: {
      loaded() {
        // 申请单id
        this.applyid = this.$parent.$parent.$parent.nodeModel.sourceid
        this.memberModel.sourceid = this.applyid
        this.memberModel.circulatedcode = this.$store.getters.user.code
        getAction(this.url.getRequirementdescription, { id: this.applyid }).then((res) => {
          if (res.success) {
            this.$set(this.memberModel, 'requirementdescription', res.result.requirementdescription)
            // 查询项目信息
            getAction(this.url.queryProjectList, { qry_projectcode: res.result.projectcode }).then((res2) => {
              if (res2.success) {
                if (res2.result.dataList.length > 0) {
                  this.$refs['projectInfo'].id = res2.result.dataList[0].id
                  this.$refs['projectInfo'].isAdd = false
                  this.$refs['projectInfo'].visible = true
                  this.$refs['projectInfo'].disableEditSubmit = true
                  this.$nextTick(_ => {
                    this.$refs['projectInfo'].open()
                  })
                }
              } else {
                this.$tap.alert(res2.message, "warning")
              }
            })
          } else {
            this.$tap.alert(res.message, "warning")
          }
        })
      },
      close() {
        this.$parent.$parent.$parent.approvalopinion = ''
      },
      confirmMember() {
        let opinion = '批准'
        let nameArr = this.memberModel.projectmembername.split(',')
        let codeArr = this.memberModel.projectmembercode.split(',')
        for (let i = 0; i < nameArr.length; i++) {
          opinion += nameArr[i] + '【' + codeArr[i] + '】、'
        }
        opinion = opinion.substr(0, opinion.length - 1) + '为该项目组成员。'
        this.$parent.$parent.$parent.approvalopinion = opinion
      },
      /** 校验是否选择了项目人员，若已选择则给审批意见赋初始值，并保存到表中 **/
      async validateMemberData(resolve, reject) {
        this.$refs.form.validate(async valid => {
          if (valid) {
            let res = await postAction(this.url.saveMemberData, this.memberModel)
            if (res.success) {
              resolve()
            } else {
              this.$parent.$parent.$parent.loading = false
              this.$tap.alert(res.message, "warning")
              reject()
            }
          } else {
            this.$parent.$parent.$parent.loading = false
            this.$tap.alert('请填写必填信息')
            reject()
          }
        })
      },
      doCancelEdit() {
        this.$emit('close')
      }
    }
  }
</script>
